//
//  GuiderVideosViewController.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/8/23.
//  Copyright © 2017年 AudioCtrip. All rights reserved.
//

#import "GuiderVideosViewController.h"
#import "ScenicGuiderListCell.h"
#import "ScenicPlayVC.h"
#import "JMHoledView.h"
#import "RelatedLinesCell.h"
@interface GuiderVideosViewController ()<UITableViewDelegate,UITableViewDataSource>


@property (nonatomic, strong) Scenic * scenic;
@end

@implementation GuiderVideosViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.barBGAlpha = 0;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 44 - kNavigationBarHeight - kBottomHeight) style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    self.subScrollView = self.tableView;
    
    self.tableView.estimatedRowHeight = 120;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
   [self.tableView registerNib:[UINib nibWithNibName:@"ScenicGuiderListCell" bundle:nil] forCellReuseIdentifier:@"ScenicGuiderListCell"];
     [self.tableView registerNib:[UINib nibWithNibName:@"RelatedLinesCell" bundle:nil] forCellReuseIdentifier:@"RelatedLinesCell"];

    [self configEmptyDataTableView:self.tableView emptyImageName:@"暂无讲解" describe:MYLocalizedString(@"SITELIST_NODATA", nil) refresh:nil block:nil];
    
    [self.tableView registerClass:[UITableViewHeaderFooterView  class] forHeaderFooterViewReuseIdentifier:@"headerView"];
    //self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
     IGLog(@"%f",self.tableView.frame.size.width);
    
    self.tableView.panGestureRecognizer.delaysTouchesBegan = NO;

}



- (void)loadDataSourceWitnScenic:(Scenic *)scenic {
    
    self.scenic = scenic;
    self.dataHasLoad = YES;
    
    [self.tableView reloadData];
}

- (void)addholedWithCell:(ScenicGuiderListCell *)cell {

    BOOL hasScenicTryListen = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasScenicTryListen"];

    if (hasScenicTryListen) {
        return;
    }

    //添加遮罩指示
    JMHoledView *holedView = [[JMHoledView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    [self.view.window addSubview:holedView];

    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];

    CGRect rect = [cell.playBtn convertRect:cell.playBtn.bounds toView:window];
//    [holedView addHoleCircleCenteredOnPosition:CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect)) diameter:CGRectGetWidth(rect) - 4];
    
    UIButton *tryBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [tryBtn setImage:[UIImage imageNamed:@"lines_play"] forState:UIControlStateNormal];
    tryBtn.frame = rect;
    [holedView addHCustomView:tryBtn onRect:rect];

    UIImage *guideImage = [UIImage imageNamed:MYLocalizedString(@"GUIDE_SCENICLIST_TRYLISTEN", nil)];

    UIImageView *imageview = [[UIImageView alloc] initWithImage:guideImage];

    CGRect guideRect = CGRectMake(CGRectGetMinX(rect) - guideImage.size.width - 5, CGRectGetMidY(rect) + 10, guideImage.size.width, guideImage.size.height);

    [holedView addHCustomView:imageview onRect:guideRect];


    __block BOOL isDone = NO;

    [holedView clickedBlock:^(JMHoledView *holedView, NSInteger index) {

        if (isDone) {
            [holedView removeFromSuperview];
        } else {

            [holedView removeHoles];

            CGRect rect = [cell convertRect:cell.bounds toView:window];

            UIImage *guideImage = [UIImage imageNamed:MYLocalizedString(@"GUIDE_SCENICLIST_DETAIL", nil)];

            UIImageView *imageview = [[UIImageView alloc] initWithImage:guideImage];

            CGRect guideRect = CGRectMake(CGRectGetMinX(rect) + (CGRectGetWidth(rect) - guideImage.size.width) / 2.0, CGRectGetMinY(rect) + (CGRectGetHeight(rect) - guideImage.size.height) / 2.0, guideImage.size.width, guideImage.size.height);

            [holedView addHCustomView:imageview onRect:guideRect];

            isDone = YES;
        }

    }];

    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasScenicTryListen"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


#pragma mark -- UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
     CGFloat offsetY = scrollView.contentOffset.y;
    if (!self.canScroll) {
        [scrollView setContentOffset:CGPointZero];
    }
    
    if (offsetY<0) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"kLeaveTopNtf" object:@1];
        self.canScroll = NO;
        scrollView.contentOffset = CGPointZero;
    }
}


#pragma mark   - UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    NSInteger count = 0;
    
    count += self.scenic.linklines.count == 0 ? 0 : 1;
    count += self.scenic.lines.count == 0 ? 0 : 1;

    return count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0 && self.scenic.lines.count != 0) {
        return self.scenic.lines.count;
    }
    return self.scenic.linklines.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0 && self.scenic.lines.count != 0) {
        ScenicGuiderListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ScenicGuiderListCell"];
        cell.superVC = self.superVC;
        LineModel *model = self.scenic.lines[indexPath.row];
        
        [cell configDataWithScenicGuiderListModel:model];
        
        
        return cell;
    } else {
        
        LineModel *model = self.scenic.linklines[indexPath.row];
        
        RelatedLinesCell * cell = [tableView dequeueReusableCellWithIdentifier:@"RelatedLinesCell"];
        [cell configDataWintLineModel:model];
        cell.superVC = self.superVC;
        
        return cell;
    }
    
}


#pragma mark -UITableViewDelegate
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.section == 0 && self.scenic.lines.count != 0) {
//        
//        return 120;
//    }else {
//        return 130;
//    }
//    
//}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath {

    if ([cell isKindOfClass:[ScenicGuiderListCell class]] && indexPath.row == 0) {

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self addholedWithCell:(ScenicGuiderListCell *)cell];
        });
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (section == 0 && self.scenic.lines.count != 0) {
        
        UITableViewHeaderFooterView * headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headerView"];
        
        headerView.backgroundColor = [UIColor clearColor];
        
        return headerView;
    } else {
        
        UITableViewHeaderFooterView * headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"relateLineHeaderView"];
        headerView.backgroundColor = [UIColor clearColor];
        if (!headerView) {
            headerView = [self tableViewHeaderView];
        }
        
        return headerView;
    }
    
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0 && self.scenic.lines.count != 0) {
        return 10;
    }else {
        
        return 57;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.0000001;
}

- (UITableViewHeaderFooterView *)tableViewHeaderView {
    
    UITableViewHeaderFooterView * headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"relateLineHeaderView"];
    
    
    UIView * bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    [headerView.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(headerView.contentView.mas_leading);
        make.trailing.equalTo(headerView.contentView.mas_trailing);
        make.bottom.equalTo(headerView.contentView.mas_bottom);
        make.height.mas_equalTo(47);
    }];
    
    UILabel * label = [[UILabel alloc] init];
    label.textColor = kTitleColor;
    label.text = MYLocalizedString(@"SITELIST_ABOUT", nil);
    label.font = Font(15);
    [bgView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(bgView.mas_leading).offset(15);
        make.centerY.equalTo(bgView.mas_centerY);
    }];
    
    return headerView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
